<template>
  <div class="page-index">
    <div class="page-header">
      <div>
        <el-radio-group v-model="activeType">
          <el-radio-button label="1">互动积分策略</el-radio-button>
          <el-radio-button label="2">积分规则说明</el-radio-button>
        </el-radio-group>
      </div>
      <div>
        <el-button size="mini" type="primary" @click="openAdd">新增</el-button>
      </div>
    </div>
    <div class="page-body">
      <div class="table-search">
        <el-form inline>
          <el-form-item label="任务名称">
            <el-input v-model="queryParams.taskName" size="mini" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="queryParams.status" size="mini" placeholder="请选择">
              <el-option label="启用" :value="1" />
              <el-option label="停用" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="mini" type="primary" @click="search">搜索</el-button>
            <el-button size="mini" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="table-box">
        <el-table
          :data="tableData"
          height="100%"
        >
          <el-table-column align="center" type="index" label="序号" width="50" />
          <el-table-column align="center" label="任务名称" min-width="150" />
          <el-table-column align="center" label="可获积分" width="120" />
          <el-table-column align="center" label="每日上限" width="120" />
          <el-table-column align="center" label="任务有效期" width="240" />
          <el-table-column align="center" label="状态" width="180">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-text="启用"
                inactive-text="停用"
                @change="(val)=>statusChange(val,scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column align="center" label="更新时间" width="160" />
          <el-table-column align="center" label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="openEdit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination
        :total="total"
        :page.sync="queryParams.pageNo"
        :limit.sync="queryParams.pageSize"
        @pagination="getData"
      />
    </div>
    <addOrEdit ref="addOrEdit" @getData="getData" />
  </div>
</template>
<script>
import addOrEdit from './components/addOrEdit'
import { queryByCondition, edit } from '@/api/cadreBusiness/IntegralSettings'
export default {
  components: { addOrEdit },
  data () {
    return {
      activeType: '1',
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        taskName: '',
        status: ''
      },
      total: 0,
      tableData: [],
      multipleSelection: []
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    search () {
      this.queryParams.pageNo = 1
      this.getData()
    },
    getData () {
      queryByCondition(this.queryParams).then((res) => {
        this.tableData = res?.data?.records || []
        this.total = res?.data?.total || 0
      })
    },
    openAdd () {
      this.$refs.addOrEdit.open({})
    },
    openEdit (row) {
      this.$refs.addOrEdit.open(row)
    },
    statusChange (val, row) {
      if (val) {
        row.status = 1
      } else {
        row.status = 0
      }
      edit({ ...row }).then((res) => {
        this.$message.success('状态修改成功')
        this.getData()
      })
    },
    reset () {
      this.queryParams.status = ''
      this.queryParams.taskName = ''
      this.search()
    }
  }
}
</script>
<style scoped lang="scss">
.page-header {
  display: flex;
  height: 60px;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #ddd;
  padding-left: 8px;
  padding-right: 8px;
}
.page-body {
  padding-top: 16px;
  // .table-search {
  //   display: flex;
  //   justify-content: space-between;
  // }
  .table-box {
    height: calc(100vh - 280px);
    .click-span {
      cursor: pointer;
      color: #409EFF;
    }
  }
}

</style>
